package com.zz.hhbl.ui.translate

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.zz.framework.core.ext.onClick
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.databinding.ItemTranslateLeftBinding
import com.zz.hhbl.databinding.ItemTranslateRightBinding
import com.zz.hhbl.entity.AiSubtitleEntity

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/3/1 20:10.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
class TranslateAdapter : BaseMultiItemAdapter<AiSubtitleEntity>() {
    val datas = mutableListOf<AiSubtitleEntity>()
    private val ITEM_TYPE_LEFT = 9998
    private val ITEM_TYPE_RIGHT = 9999

    private class ItemTranslateLeftViewHolder(val viewBinding: ItemTranslateLeftBinding) : RecyclerView.ViewHolder(viewBinding.root)
    private class ItemTranslateRightViewHolder(val viewBinding: ItemTranslateRightBinding) : RecyclerView.ViewHolder(viewBinding.root)

    init {
        addItemType(ITEM_TYPE_LEFT, object : OnMultiItemAdapterListener<AiSubtitleEntity, ItemTranslateLeftViewHolder> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemTranslateLeftViewHolder {
                val viewBinding = ItemTranslateLeftBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemTranslateLeftViewHolder(viewBinding)
            }

            override fun onBind(holder: ItemTranslateLeftViewHolder, position: Int, item: AiSubtitleEntity?) {
                holder.viewBinding.tvContent.apply {
                    onClick {
                        item?.let {
                            mListener?.invoke(item)
                        }
                    }
                    text = item?.text.orEmpty()
                    (parent as ViewGroup).clipChildren = false
                }
            }
        }).addItemType(ITEM_TYPE_RIGHT, object : OnMultiItemAdapterListener<AiSubtitleEntity, ItemTranslateRightViewHolder> {
            override fun onBind(holder: ItemTranslateRightViewHolder, position: Int, item: AiSubtitleEntity?) {
                holder.viewBinding.tvContent.apply {
                    text = item?.text.orEmpty()
                    (parent as ViewGroup).clipChildren = false
                }
            }

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemTranslateRightViewHolder {
                val viewBinding = ItemTranslateRightBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemTranslateRightViewHolder(viewBinding)
            }
        }).onItemViewType { position, list ->
            if (list[position].userId == GlobalCache.getAiConfig().userId) {
                ITEM_TYPE_RIGHT
            } else {
                ITEM_TYPE_LEFT
            }
        }
    }

    var mListener: ((item: AiSubtitleEntity) -> Unit)? = null
    fun setItemClickListener(listener: (item: AiSubtitleEntity) -> Unit) :TranslateAdapter{
        mListener = listener
        return this
    }

    fun getData() = datas

    override fun submitList(list: List<AiSubtitleEntity>?) {
        if (list.isNullOrEmpty()) {
            datas.clear()
        }
        super.submitList(datas)
    }

    fun addData(item: AiSubtitleEntity) {
        if (datas.isNullOrEmpty().not()) {
            if (datas.last().definite) {
                datas.add(item)
            } else {
                datas[items.lastIndex] = item
            }
        } else {
            datas.add(item)
        }
        submitList(datas)
    }

}